
package com.bi2.events.mobile;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class SearchFriendsActivity extends Activity {
    /** Called when the activity is first created. */
    private ListView lv1;
    private EditText ed;
    private String lv_arr[] = {
            "Cassio Augusto", "Rafael Mendes", "Robson Junior",
            "Rodrigo"
    };
    private ArrayList<String> arr_sort = new ArrayList<String>();
    int textlength = 0;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.search_friends);
        lv1 = (ListView) findViewById(R.id.friends_search_listview);
        ed = (EditText) findViewById(R.id.search_friends_searchbox);

        lv1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lv_arr));
        ed.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            public void onTextChanged(CharSequence s, int start, int before, int count) {

                textlength = ed.getText().length();
                arr_sort.clear();
                for (int i = 0; i < lv_arr.length; i++) {
                    if (textlength <= lv_arr[i].length()) {
                        if (ed.getText().toString()
                                .equalsIgnoreCase((String) lv_arr[i].subSequence(0, textlength))) {
                            arr_sort.add(lv_arr[i]);
                        }
                    }
                }

                lv1.setAdapter(new ArrayAdapter<String>(SearchFriendsActivity.this,
                        android.R.layout.simple_list_item_1, arr_sort));

            }
        });
    }

}
